package com.hbt.orderfood.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.orderfood.R;
import com.hbt.orderfood.adapter.UserFoodAdapter;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.entitys.Order;
import com.hbt.orderfood.utils.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    String type = StringUtils.MAIN;
    @BindView(R.id.recycle)
    RecyclerView recycle;
    @BindView(R.id.main)
    TextView main;
    @BindView(R.id.veg)
    TextView veg;
    @BindView(R.id.drink)
    TextView drink;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.total_price)
    TextView totalPrice;
    @BindView(R.id.myorder_btn)
    TextView myorderBtn;
    private UserFoodAdapter userFoodAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initRecycler();
        changeLeftMenuColor();
        main.setTextColor(getResources().getColor(R.color.leftMenu_select_color));
    }

    void initRecycler() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle.setLayoutManager(layoutManager);
        userFoodAdapter = new UserFoodAdapter(new DBAdapter(MainActivity.this).getFoodsByType(type));
        recycle.setAdapter(userFoodAdapter);
        userFoodAdapter.setListener(new UserFoodAdapter.OnClickAddBtnListener() {
            @Override
            public void clickAddBtn() {
                int price = 0;
                for (Order o : StringUtils.orders) {
                    price += o.getPrice();
                }
                totalPrice.setText("总计：" + String.valueOf(price) + "￥");
                Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_LONG).show();
            }
        });

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        int price = 0;
        for (Order o : StringUtils.orders) {
            price += o.getPrice();
        }
        totalPrice.setText("总计：" + String.valueOf(price) + "￥");
    }

    void changeLeftMenuColor() {
        int color = 0xFFFFFFFF;
        main.setTextColor(color);
        veg.setTextColor(color);
        drink.setTextColor(color);
    }

    @OnClick({R.id.main, R.id.veg, R.id.drink})
    public void onViewClicked(View view) {
        changeLeftMenuColor();
        String type_str = StringUtils.MAIN_STR;
        switch (view.getId()) {

            case R.id.main:
                type = StringUtils.MAIN;
                main.setTextColor(getResources().getColor(R.color.leftMenu_select_color));
                break;
            case R.id.veg:
                type = StringUtils.VEG;
                veg.setTextColor(getResources().getColor(R.color.leftMenu_select_color));
                type_str = StringUtils.VEG_STR;
                break;
            case R.id.drink:
                type = StringUtils.DRINK;
                drink.setTextColor(getResources().getColor(R.color.leftMenu_select_color));
                type_str = StringUtils.DRINK_STR;
                break;
        }
        title.setText(type_str);
        List<Food> foods = new DBAdapter(MainActivity.this).getFoodsByType(type);
        ((UserFoodAdapter) recycle.getAdapter()).setFoods(foods);
        recycle.getAdapter().notifyDataSetChanged();
    }//onViewClicked

    @OnClick(R.id.myorder_btn)
    void MyOrderClick(){
        Intent intent = new Intent(MainActivity.this, MyOrderActivity.class);
        startActivity(intent);
    }


}//class
